var { ReturnModel } = require('../libs/resmodel');
var { settingAdd,
    settingUpdate,
    settingInfoByName,
    settingList,gettime, usercancel } = require('../services/setting');

/**
* 添加系统配置
* @api {post} /setting/add 添加系统配置
* @apiDescription 添加系统配置
* @apiName add
* @apiParam (参数) {String} name 配置名称
* @apiParam (参数) {String} datastr 配置值
* @apiSampleRequest /setting/add
* @apiGroup setting-系统配置
* @apiVersion 1.0.0
* @apiParamExample {json} Request-Example:
*     {
*       "name": "热门城市",  // 配置名称
*       "datastr": "深圳,广州,北京,上海",  // 配置值
*     }
* @apiSuccessExample {json} Success-Response:
*   HTTP/1.1 200 OK
*   {
*      "msg": "添加成功",
*      "code": 0
*   }
*/
async function add(req, res) {
    let result = await settingAdd(req.body);
    res.json(new ReturnModel(result));
}
/**
* 修改系统配置
* @api {post} /setting/edit 修改系统配置
* @apiDescription 修改系统配置
* @apiName edit
* @apiParam (参数) {String} name 配置名称
* @apiParam (参数) {String} datastr 配置值
* @apiSampleRequest /setting/edit
* @apiGroup setting-系统配置
* @apiVersion 1.0.0
* @apiParamExample {json} Request-Example:
*     {
*       "id": 23,  // 配置id
*       "name": "热门城市",  // 配置名称
*       "datastr": "深圳,广州,北京,上海",  // 配置值
*     }
* @apiSuccessExample {json} Success-Response:
*   HTTP/1.1 200 OK
*   {
*      "msg": "修改成功",
*      "code": 0
*   }
*/
async function edit(req, res) {
    let result = await settingUpdate(req.body);
    res.json(new ReturnModel(result));
}
/**
* 查询系统配置
* @api {post} /setting/querybyname 查询系统配置
* @apiDescription 修改系统配置
* @apiName querybyname
* @apiParam (参数) {String} name 配置名称
* @apiSampleRequest /setting/querybyname
* @apiGroup setting-系统配置
* @apiVersion 1.0.0
* @apiParamExample {json} Request-Example:
*     {
*       "name": "热门城市",  // 配置名称、每日推荐人数
*     }
* @apiSuccessExample {json} Success-Response:
*   HTTP/1.1 200 OK
*   {
*        data: {
*          "name": "热门城市", // 配置名称
*          "datastr": "深圳,广州,北京,上海", // 配置值
*          "createtime": "2022-04-02 15:23:22", // 创建时间
*          "updatetime": "2022-04-02 15:23:22", // 更新时间
*        },
*      "msg": "修改成功",
*      "code": 0
*   }
*/
async function querybyname(req, res) {
    let result = await settingInfoByName(req.body);
    res.json(new ReturnModel(result));
}
/**
* 获取系统配置列表
* @api {post} /setting/list 获取系统配置列表
* @apiDescription 获取系统配置列表
* @apiName list
* @apiParam (参数) {Number} pagenum 查询第几页(默认第1页)
* @apiParam (参数) {Number} pagesize 每页显示条数(默认10条)
* @apiSampleRequest /setting/list
* @apiGroup setting-系统配置
* @apiVersion 1.0.0
* @apiParamExample {json} Request-Example:
*     {
*       "pagenum": 1,  // 查询第几页
*       "pagesize": 10,  // 每页显示条数
*     }
* @apiSuccessExample {json} Success-Response:
*   HTTP/1.1 200 OK
*   {
*      "data": 
*        list: [{
*          "name": "热门城市", // 配置名称
*          "datastr": "深圳,广州,北京,上海", // 配置值
*          "createtime": "2022-04-02 15:23:22", // 创建时间
*          "updatetime": "2022-04-02 15:23:22", // 更新时间
*        }],
*        "total": 200,//总条数
*        "pagecount": 20,//总页数
*        }
*      "msg": "获取成功",
*      "code": 0
*   }
*/
async function list(req, res) {
    let result = await settingList(req.body);
    res.json(new ReturnModel(result));
}
/**
* 推荐剩余时间
* @api {get} /setting/getTime 推荐剩余时间
* @apiDescription 推荐剩余时间
* @apiName getTime
* @apiSampleRequest /setting/getTime
* @apiGroup setting-系统配置
* @apiVersion 1.0.0
* @apiSuccessExample {json} Success-Response:
*   HTTP/1.1 200 OK
*   {
*      "data": 67216044,
*      "code": 0
*   }
*/
function getTime(req, res){
    let result = gettime();
    res.json(new ReturnModel(result));
}

function cancel(req, res){
    let result = usercancel(req.body);
    res.json(new ReturnModel(result));
}

module.exports = function (app) {
    let base_url = '/api/setting';

    app.post(base_url+'/list', list);
    app.post(base_url+'/add', add);
    app.post(base_url+'/edit', edit);
    app.post(base_url+'/querybyname', querybyname);
    app.get(base_url+'/getTime', getTime);
    app.post('/api/set/cancel', cancel);
}
